import requests
import io
import base64
from PIL import Image

def get_pic_from_stable_diffusion(payload,pic_name):
    url = "http://127.0.0.1:7860"
    response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload)

    r = response.json()
    #print(r)  #测试是否成功调用接口生成图像，成功应出现一堆乱码
    for i in r['images']:
        image = Image.open(io.BytesIO(base64.b64decode(i.split(",", 1)[0])))
        image.save(f'images/{pic_name}')